from bs4 import BeautifulSoup
import pandas as pd
import numpy as np
import requests
import time
def getHtml(url):
    try:
        r=requests.get(url)
        r.raise_for_status()
        r.encoding=r.apparent_encoding
        return r.text
    except:
        return "读取失败"
def get_url(ui,n):
    ulist=[]
    for i in range(1,n+1):
        ulist.append(ui+str(i))
    return ulist
def get_data(u):
   r=getHtml(u)
   soup=BeautifulSoup(r,"lxml")
   data=soup.find('ul',class_='list_item clrfix').find_all('li')
   datafor=[]
   for i in data:
       dicts={}
       dicts['lat']=i['data-lat']
       dicts['lng']=i['data-lng']
       dicts['景点名称']=i.find('span',class_='cn_tit').text
       dicts['攻略数量'] = i.find('div', class_="strategy_sum").text
       dicts['点评数量'] = i.find('div', class_="comment_sum").text
       dicts['景点排名'] = i.find('span', class_="ranking_sum").text
       dicts['驴友去过'] = i.find('span', class_="sum").text
       datafor.append(dicts)
   return datafor
def main():
    ulist=get_url("https://travel.qunar.com/p-cs299878-shanghai-jingdian-1-",50)
    data=[]
    for i in ulist:
        data.extend(get_data(i))
        time.sleep(0.3)
    p=pd.DataFrame(data)
    p.to_csv(r"C:/Users/gcc/Desktop/jingqu.csv", encoding="utf-8")
    print(p)
main()

